package com.work.music.dao;

import com.work.music.entity.MusicCollectPraise;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface MusicCollectPraiseMapper {
    @Delete({
            "delete from music_collect_praise",
            "where uid = #{uid,jdbcType=BIGINT}",
            "and type = #{type,jdbcType=INTEGER}",
            "and typeid = #{typeid,jdbcType=BIGINT}",
            "and peration = #{peration,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(@Param("uid") Long uid, @Param("type") Integer type, @Param("typeid") Long typeid, @Param("peration") Integer peration);

    @Insert({
            "insert into music_collect_praise (uid, type, ",
            "typeid, peration, ",
            "status, addtime, ",
            "addip)",
            "values (#{uid,jdbcType=BIGINT}, #{type,jdbcType=INTEGER}, ",
            "#{typeid,jdbcType=BIGINT}, #{peration,jdbcType=INTEGER}, ",
            "#{status,jdbcType=INTEGER}, #{addtime,jdbcType=TIMESTAMP}, ",
            "#{addip,jdbcType=VARCHAR})"
    })
    int insert(MusicCollectPraise record);

    @Select({
            "select",
            "uid, type, typeid, peration, status, addtime, addip",
            "from music_collect_praise",
            "where uid = #{uid,jdbcType=BIGINT}",
            "and type = #{type,jdbcType=INTEGER}",
            "and typeid = #{typeid,jdbcType=BIGINT}",
            "and peration = #{peration,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "uid", property = "uid", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "type", property = "type", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "typeid", property = "typeid", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "peration", property = "peration", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "addtime", property = "addtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "addip", property = "addip", jdbcType = JdbcType.VARCHAR)
    })
    MusicCollectPraise selectByPrimaryKey(@Param("uid") Long uid, @Param("type") Integer type, @Param("typeid") Long typeid, @Param("peration") Integer peration);

    @Select({
            "select",
            "uid, type, typeid, peration, status, addtime, addip",
            "from music_collect_praise"
    })
    @Results({
            @Result(column = "uid", property = "uid", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "type", property = "type", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "typeid", property = "typeid", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "peration", property = "peration", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "addtime", property = "addtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "addip", property = "addip", jdbcType = JdbcType.VARCHAR)
    })
    List<MusicCollectPraise> selectAll();

    @Update({
            "update music_collect_praise",
            "set status = #{status,jdbcType=INTEGER},",
            "addtime = #{addtime,jdbcType=TIMESTAMP},",
            "addip = #{addip,jdbcType=VARCHAR}",
            "where uid = #{uid,jdbcType=BIGINT}",
            "and type = #{type,jdbcType=INTEGER}",
            "and typeid = #{typeid,jdbcType=BIGINT}",
            "and peration = #{peration,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(MusicCollectPraise record);

    /**
     * 获取收藏列表信息
     *
     * @param uid
     * @param start
     * @param count
     * @return
     */
    @Select({
            "select",
            "uid, type, typeid, peration, status, addtime, addip",
            "from music_collect_praise",
            "where",
            "uid=#{uid} and type=2 and peration=1 and status=1",
            "order by addtime desc limit #{start},#{count}"
    })
    @Results({
            @Result(column = "uid", property = "uid", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "type", property = "type", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "typeid", property = "typeid", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "peration", property = "peration", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "addtime", property = "addtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "addip", property = "addip", jdbcType = JdbcType.VARCHAR)
    })
    List<MusicCollectPraise> getAllByUid(@Param("uid") long uid, @Param("start") int start, @Param("count") int count);

    /**
     * 获取操作总条数
     *
     * @param type
     * @param typeid
     * @param peration
     * @return
     */
    @Select("select ifnull(count(*), 0) from music_collect_praise " +
            "where " +
            "type=#{type} and typeid=#{typeid} and peration=#{peration} and status=1")
    int selectCountByTypeId(@Param("type") int type, @Param("typeid") long typeid, @Param("peration") int peration);

    /**
     * 获取是否操作
     * @param type
     * @param typeid
     * @param peration
     * @param uid
     * @return
     */
    @Select("select ifnull(count(*), 0) from music_collect_praise " +
            "where " +
            "type=#{type} and typeid=#{typeid} and peration=#{peration} and uid=#{uid} and status=1")
    int selectCountByTypeAndTypeIdAndUid(@Param("type") int type, @Param("typeid") long typeid, @Param("peration") int peration, @Param("uid") long uid);
}